@file:Suppress("RemoveEmptyParenthesesFromAnnotationEntry")

package com.gitee.wsl.compose.ui.richtext

import androidx.compose.foundation.layout.Arrangement.spacedBy
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocal
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import com.gitee.wsl.compose.ui.richtext.block.list.RestartListLevel
import com.gitee.wsl.compose.ui.richtext.handler.LinkClickHandler
import com.gitee.wsl.compose.ui.richtext.handler.LocalLinkClickHandler
import com.gitee.wsl.compose.ui.richtext.theme.RichTextStyle
import com.gitee.wsl.compose.ui.richtext.theme.WithStyle
import com.gitee.wsl.compose.ui.richtext.theme.currentRichTextStyle
import com.gitee.wsl.compose.ui.richtext.theme.resolveDefaults

/**
 * Scope object for composables that can draw rich text.
 *
 * RichTextScope facilitates a context for RichText elements. It does not
 * behave like a [State] or a [CompositionLocal]. Starting from [BasicRichText],
 * this scope carries information that should not be passed down as a state.
 */

object RichTextScope

/**
 * Draws some rich text. Entry point to the compose-richtext library.
 */
@Composable
fun BasicRichText(
    modifier: Modifier = Modifier,
    style: RichTextStyle? = null,
    linkClickHandler: LinkClickHandler? = null,
    children: @Composable RichTextScope.() -> Unit
) {
  with(RichTextScope) {
    RestartListLevel {
      WithStyle(style) {
        CompositionLocalProvider(LocalLinkClickHandler provides linkClickHandler) {
          val resolvedStyle = currentRichTextStyle.resolveDefaults()
          val blockSpacing = with(LocalDensity.current) {
            resolvedStyle.paragraphSpacing!!.toDp()
          }

          Column(modifier = modifier, verticalArrangement = spacedBy(blockSpacing)) {
            children()
          }
        }
      }
    }
  }
}


